package com.cq.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.support.Parameter;
import com.cq.health.common.MessageConst;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.entity.Result;
import com.cq.health.pojo.CheckGroup;
import com.cq.health.service.CheckGroupService;
import com.cq.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/5/12 15:48
 * @Description: 检查组控制器
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup 检查组
     * @param checkItemIds 选中的检查项
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds){
        try {
            checkGroupService.add(checkGroup,checkItemIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = new PageResult(0L, new ArrayList());
        try {
            pageResult = checkGroupService.findByPage(queryPageBean);
            log.debug("----------------pageResult{}"+pageResult);
        }catch (Exception e){
             e.printStackTrace();
            pageResult = new PageResult(0L,new ArrayList());
        }
        return pageResult;
    }

    /**
     * 更新数据回显
     * @return
     */
    @RequestMapping("/updateUI")
    public Result updateUI(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据检查组id查询检查项
     * @param checkGroupId
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        try {
            List<Integer> ids = checkGroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
            return new Result(true,MessageConst.ACTION_SUCCESS,ids);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 更新检查组
     * @param checkitemIds 选中的检查项
     * @param checkGroup 检查组
     * @return
     */
    @RequestMapping("/update")
    public Result update(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.update(checkitemIds,checkGroup);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询全部检查组
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

}
